package com.corpgovernment.travelstandard.impl;

import com.corpgovernment.common.utils.MybatisBatchUtils;
import com.corpgovernment.travelstandard.entity.db.MbTravelStandCarCity;
import com.corpgovernment.travelstandard.entity.db.MbTravelStandCarCrossCity;
import com.corpgovernment.travelstandard.mapper.MbTravelStandCarCrossCityMapper;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class MbTravelStandCarCrossCityService {

    @Autowired
    private MbTravelStandCarCrossCityMapper mbTravelStandCarCrossCityMapper;

    @Autowired
    private MybatisBatchUtils mybatisBatchUtils;

    public List<MbTravelStandCarCrossCity> selectByTsid(String tsId, String tsType) {
        Example example = new Example(MbTravelStandCarCrossCity.class);
        example.createCriteria().andEqualTo("tsid", tsId).andEqualTo("tsType", tsType);
        return mbTravelStandCarCrossCityMapper.selectByExample(example);
    }

    public List<MbTravelStandCarCrossCity> selectByTsids(List<Long> tsIds, String tsType) {
        Example example = new Example(MbTravelStandCarCrossCity.class);
        example.createCriteria().andIn("tsid", tsIds).andEqualTo("tsType", tsType);
        return mbTravelStandCarCrossCityMapper.selectByExample(example);
    }

    public boolean saveBatch(List<MbTravelStandCarCrossCity> carCrossCityList) {
        if (CollectionUtils.isEmpty(carCrossCityList)) {
            return false;
        }
        long result = mybatisBatchUtils.batchUpdateOrInsert(carCrossCityList, MbTravelStandCarCrossCityMapper.class, (item, mbTravelStandCarCrossCityMapper) -> mbTravelStandCarCrossCityMapper.insertSelective(item));
        return result > 0;
    }

    public boolean delByTsid(String tsId, String tsType) {
        Example example = new Example(MbTravelStandCarCrossCity.class);
        example.createCriteria().andEqualTo("tsid", tsId).andEqualTo("tsType", tsType);
        int result = mbTravelStandCarCrossCityMapper.deleteByExample(example);
        return result > 0;
    }
}
